﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HotelManagementModel;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Data;

namespace HotelManagement.Repository
{
   public  class Repository : IRepository
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=HotelManageMentDatabase;Integrated Security=True");
        #region 删除User表
        public int UserDelete(int id)
        {
            con.Open();
            SqlCommand com = new SqlCommand("delete UserTable where UserId='" + id + "'", con);
            int a = com.ExecuteNonQuery();
            con.Close();
            return a;
        }
        #endregion

        #region 添加User表
        public int UserInser(User u)
        {
            con.Open();
            SqlCommand com = new SqlCommand("insert into UserTable values('"+u.UserName+"','"+u.UserSex+"','"+u.UserAge+"','"+u.UserPhone+"','"+u.UserAddress+"')", con);
            int a = com.ExecuteNonQuery();
            con.Close();
            return a;
        }
        #endregion

        #region 修改User表
        public int UserUpdate(User u)
        {
            con.Open();
            SqlCommand com = new SqlCommand("Update UserTable set UserName='" + u.UserName + "',UserSex='" + u.UserSex + "',UserAge'" + u.UserAge + "',UserPhone'" + u.UserPhone + "',UserAddress='" + u.UserAddress + "'", con);
            int a = com.ExecuteNonQuery();
            con.Close();
            return a;
        }
        #endregion

        #region 查询User表
        //查询User表
        public IList<User> UserSelect()
        {
            con.Open();
            SqlDataAdapter dr = new SqlDataAdapter("select * from UserTable", con);
            DataTable br = new DataTable();
            dr.Fill(br);
            con.Close();
            var json = JsonConvert.SerializeObject(br);
            List<User> list = JsonConvert.DeserializeObject<List<User>>(json);
            return list;
        }

        public int UserUpdate(User u)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
